package nl.saxion.webtech.roulette.model;

import java.util.ArrayList;

public class Lobby {

	private static Lobby		uniqueInstance;

	public ArrayList<Table>		tables;
	public ArrayList<Player>	players;

	private Lobby() {
		tables = new ArrayList<Table>();
		tables.add( new Table( 1 ) );
		tables.add( new Table( 2 ) );
		tables.add( new Table( 3 ) );
		tables.add( new Table( 4 ) );
		tables.add( new Table( 5 ) );
		tables.add( new Table( 6 ) );
		players = new ArrayList<Player>();
	}

	public static Lobby getInstance() {
		if( uniqueInstance == null ) {
			uniqueInstance = new Lobby();
			
			// TODO: register default accounts??
			uniqueInstance.register( "Rick", "test", "test" );
			uniqueInstance.register( "Dennis", "test", "test" );
		}
		return uniqueInstance;
	}

	public ArrayList<Player> getPlayers() {
		return players;
	}
	
	public Table getTable( int tableId ) {
		for( Table table : tables ) {
			if( table.getTableId() == tableId ) {
				return table;
			}
		}
		return null;
	}

	public Player getPlayer( int playerId ) {
		for( Player player : players ) {
			if( player.getPlayerId() == playerId ) {
				return player;
			}
		}
		return null;
	}

	public void topUpCashAmountForPlayer( int playerId ) {
		Player player = getPlayer( playerId );
		double cash = player.getAmountOfCash();
		if( cash == 0 ) {
			player.setAmountOfCash( 2000 );
		} else {
			System.out.println( "You can only up your cash amount if you're out of moneyz." );
		}
	}
	
	public Player logIn( String username, String password ) {
		for( Player player : players ) {
			if( player.getUsername().equalsIgnoreCase( username ) ) {
				if( player.getPassword().equalsIgnoreCase( password ) ) {
					player.setLoggedIn( true );
					return player;
				} else {
					player.setLoggedIn( false );
				}
			}
		}
		return null;
	}
	
	public boolean LogOut( int playerId ) {
		for( Player player : players ) {
			if( player.getPlayerId() == playerId ) {
				player.setLoggedIn( false );
				return true;
			}
		}
		return false;
	}
	
	public boolean register( String username, String password, String confirmPassword ) {
		if(password.equalsIgnoreCase(confirmPassword)){
			players.add( new Player( username, password ) );
			return true;
		} else {
			System.out.println("Passwords do not match!");
			return false;
		}
	}
}
